<div class="container-fluid">
    <h4 class="content-head">管理人员信息</h4>
    <div class="table-wrapper users-table section">
        <div class="input-group" id="search">
            &nbsp;<label for="username">登录名：</label>
            <input type="text" name="username" class="input-medium" id="username">
            <label for="realname">&nbsp;姓名：</label>
            <input type="text" name="realname" class="input-medium" id="realname">
       <span class="btn_area">
           <button id="search_btn" type="button" class="btn btn-purple btn-sm">
               搜索
               <i class="icon-search icon-on-right bigger-110"/>
           </button>
       </span>
        </div>
        <div id="list"></div>
    </div>
</div>

<script type="text/javascript">
    (function () {
        var url = 'secUser/list-data';
        var $datagrid = $('#list');

        loadData();

        $('#search_btn').on('click', function () {
            loadData();
        });

        function loadData() {
            $datagrid.datagrid({
                checkbox: true,
                toolbar: [
                    {
                        type: 'ADD',
                        click: function () {
                            addSecUser();
                        }
                    },
                     {
                    type: 'MOD',
                    click: function () {
                        var datas = $datagrid.selectedRowDatas();
                        if (datas && datas.length == 1) {
                            doUpateData(datas[0].id);
                        } else {
                            layer.msg('请选择一条数据', {icon: 5})
                            }
                        }
                    }
                ],
                'dataModel': {
                    'url': url,
                    'parameters': getCommJsonParams('search')
                },
                'columns': [
                    {title: '登录名', dataField: 'username'},
                    {title: '真实姓名', dataField: 'realname'},
                    {title: '状态', dataField: 'statusDict'},
                    {title: '创建时间', dataField: 'createTime'}
                ]
            });
        };

        function addSecUser() {
            var $div = $('<div>');
            $div.load('secUser/form', function (response, status, xhr) {
                if (xhr.status == 200) {
                    layer.open({
                        type: 1,//Page层类型
                        area: ['400px', '410px'],
                        title: '增加管理员',
                        btn: ['提交'],
                        btnAlign: 'c',
                        content: $div.html(),
                        yes: function (layId) {
                            var param = $('#secUserForm').serialize();
                            $.post('secUser/add', param, function (result) {
                                if (result.success) {
                                    loadData();
                                    layer.msg('新增成功', {icon: 1});
                                    layer.close(layId)
                                } else {
                                    layer.msg(result.message, {icon: 5});
                                }
                            }, 'json')
                        }
                    });
                }
            });
        }

        function doUpateData(id) {
            var $div = $('<div>');
            $div.load('secUser/toUpdate?id=' + id, function (response, status, xhr) {
                if (xhr.status == 200) {
                    layer.open({
                        type: 1,//Page层类型
                        area: ['400px', '410px'],
                        title: '修改管理员信息',
                        btn: ['提交修改'],
                        btnAlign: 'c',
                        content: $div.html(),
                        yes: function (layId) {
                            var param = $('#secUserFormModify').serialize();
                            $.post('secUser/update', param, function (result) {
                                if (result.success) {
                                    loadData();
                                    layer.msg('修改成功', {icon: 1});
                                    layer.close(layId)
                                } else {
                                    layer.msg(result.message, {icon: 5});
                                }
                            }, 'json')
                        }
                    });
                }
            });
        }

    })();
</script>
